package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.b2.mapper.CourseDimensionMapper;
import com.b2.pojo.entity.CourseDimension;
import com.b2.service.CourseDimensionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 课程维度表(CourseDimension)表服务实现类
 *
 * @author makejava
 * @since 2024-05-15 10:14:15
 */
@Service("courseDimensionService")
public class CourseDimensionServiceImpl extends ServiceImpl<CourseDimensionMapper, CourseDimension> implements CourseDimensionService {

    @Resource
    private CourseDimensionMapper courseDimensionMapper;

    @Override
    @Transactional
    public void update(CourseDimension courseDimension) {
        extracted(courseDimension,true);
        // 修改数据
        courseDimension.setUpdateTime(LocalDateTime.now());
        courseDimensionMapper.updateById(courseDimension);
    }

    @Override
    @Transactional
    public void add(CourseDimension courseDimension) {
        extracted(courseDimension, false);
        // 插入数据
        courseDimension.setUpdateTime(LocalDateTime.now());
        courseDimensionMapper.insert(courseDimension);
    }

    @Override
    public void delete(Long dimensionId) {
        LambdaQueryWrapper<CourseDimension> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseDimension::getParentId, dimensionId);
        List<CourseDimension> courseDimensions = courseDimensionMapper.selectList(lambdaQueryWrapper);
        if(courseDimensions.size() != 0) {
            throw new RuntimeException(MessageConstant.NOT_ALLOW_DELETE);
        }
        courseDimensionMapper.deleteById(dimensionId);
    }

    private void extracted(CourseDimension courseDimension, boolean flag) {
        // 判断描述和编码是否已经存在
        if(courseDimension.getParentId() == null) {
            throw new RuntimeException(MessageConstant.PARENT_ID_NULL);
        }

        LambdaQueryWrapper<CourseDimension> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseDimension::getDimensionCode, courseDimension.getDimensionCode());
        // 如果是修改，查询时不包括自身
        if(flag) {
            lambdaQueryWrapper.ne(CourseDimension::getDimensionId, courseDimension.getDimensionId());
        }
        CourseDimension courseDimension1 = courseDimensionMapper.selectOne(lambdaQueryWrapper);
        if(courseDimension1 != null) {
            throw new RuntimeException(MessageConstant.DIMENSION_CODE_EXIST);
        }

        LambdaQueryWrapper<CourseDimension> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(CourseDimension::getDescription, courseDimension.getDescription());
        // 如果是修改，查询时不包括自身
        if(flag) {
            lambdaQueryWrapper1.ne(CourseDimension::getDimensionId, courseDimension.getDimensionId());
        }
        CourseDimension courseDimension2 = courseDimensionMapper.selectOne(lambdaQueryWrapper1);
        if(courseDimension2 != null) {
            throw new RuntimeException(MessageConstant.DIMENSION_DESCRIPTION_EXIST);
        }

        // 判断编码格式是否正确
        String regex6 = "^[A-Z]{1,4}[0-9]{2}$"; // 长度位6，第5.6为数字
        String regex7 = "^[A-Z]{1,4}[0-9]{2}[A-Z]$"; // 长度7，第5、6位为数字
        String regex8 = "^[A-Z]{4}[0-9]{2}[A-Z][0-9]$"; // 长度8，第5、6、8位为数字
        String DimensionCode = courseDimension.getDimensionCode();
        int num = DimensionCode.length();
        Pattern pattern = null;
        if(num == 6) {
            pattern = Pattern.compile(regex6);
        } else if(num == 7) {
            pattern = Pattern.compile(regex7);
        } else if(num == 8) {
            pattern = Pattern.compile(regex8);
        } else {
            throw new RuntimeException(MessageConstant.DIMENSION_CODE_NUM_ERROR);
        }
        Matcher matcher = pattern.matcher(DimensionCode);
        if (!matcher.matches()) {
            throw new RuntimeException(MessageConstant.DIMENSION_CODE_ERROR);
        }
    }


}

